<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">
<style>
    #commentForm .main-container .short_table_list tbody{
        max-height: 600px;
    }
</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑合同项预算外数据</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">


        <div class="block-generic" id="caiDiv" budgetid="0">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>预置外数据明细<span class="colorhong">（预算项的计量单位与合同项计量单位不一致，系统将自动修改为预算项计量单位）</span></span></div>
                        <div>
                            <button type="button" id="list_xuan" onclick="choose_engin_budget_list_mul()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">批量选择</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45"><input type="checkbox" lay-filter="allxuan" lay-skin="primary"></th>
                            <th width="45">序号</th>
                            <th><text>*</text>成本科目</th>
                            <th>预算项</th>
                            <th>名称及规格</th>
                            <th>单位</th>
                            <th>采购数量</th>
                            <th>采购单价(元)</th>
                            <th>采购金额(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>

    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    var param_budgetid;



    function choose_engin_budget_list_mul() {
        var xuannum = 0;
        $('#list_list .allxuan').each(function (i) {
            if($(this).prop("checked")){
                xuannum += 1;
            }
        });
        if(xuannum < 1){
            facade.error("请在明细选择要批量操作的数据");return false;
        }
        openPage('../public/choose_engin_budget_list.php?choose_type=radio&page=yuwai_muil&budgetid='+param_budgetid,'80%','80%');
    }

    function choose_engin_budget_list(obj){
        var domkey = $(obj).parents('tr').index();
        openPage('../public/choose_engin_budget_list.php?choose_type=radio&page=yuwai&budgetid='+param_budgetid+'&domkey='+domkey,'80%','80%');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_engin_budget_zhi_yuwai, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var list = res.data.list;
                param_budgetid = res.data.data.id;
                var str = '';
                for (var key in list){
                    var nums = parseInt(key)+1;
                    str += '<tr>';
                    str += '<td style="width: 45px"><input type="checkbox" class="allxuan" lay-skin="primary"></td>';
                    str += '<td style="width: 65px;text-align: center">'+nums+'</td>';
                    str += '<td>';
                    str += '<input type="text" onclick="choose_engin_budget_list(this)" readonly value="'+list[key].subname+'" placeholder="请选择" id="'+list[key].id+'" listid="'+list[key].listid+'" class="layui-input subname disabled">';
                    str += '</td>';
                    str += '<td><input type="text" disabled class="layui-input guiname disabled" value="'+list[key].guiname+'"></td>';
                    str += '<td><input type="text" disabled class="layui-input name disabled" value="'+list[key].name+'"></td>';
                    str += '<td><input type="text" disabled class="layui-input unit disabled" value="'+list[key].unit+'"></td>';
                    str += '<td><input type="number" disabled class="layui-input num disabled" value="'+list[key].num+'"></td>';
                    str += '<td><input type="number" disabled class="layui-input price disabled" value="'+list[key].price+'"></td>';
                    str += '<td><input type="number" disabled class="layui-input money disabled" value="'+list[key].money+'"></td>';
                    str += '<td><input type="text" disabled class="layui-input explain disabled" value="'+list[key].explain+'"></td>';
                    str += '</tr>';
                }
                $("#list_list").html(str);

                render_from();
                initParseMoney();
            }
        });


        form.on('checkbox(allxuan)',function (data) {
            if($(data.elem).prop("checked")){
                $('#list_list .allxuan').prop("checked", true);
            }else{
                $('#list_list .allxuan').prop("checked", false);
            }
            render_from();
        });

        form.on('submit(add)', function(data){

            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var id = $(this).find('.subname').attr('id') || 0;
                    var listid = $(this).find('.subname').attr('listid');
                    var unit = $(this).find('.unit').val();


                    list.push({
                        id:id,
                        listid:listid,
                        unit:unit,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            facade.ajax({url: ask_engin_budget_zhi_yuwai, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    parent.location.reload();
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });

</script>
</body>
</html>
